/**
 * May 2, 2014 10:38:29 AM
 */
package elitech.vietnam.copyweatherfashion.adapters;

import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;
import android.view.ViewGroup;
import elitech.vietnam.copyweatherfashion.MainActivity;
import elitech.vietnam.copyweatherfashion.fragments.today.TodayPagerFragment;

/**
 * @author Cong
 *
 */
public class TodayPagerAdapter extends FragmentStatePagerAdapter {
	
	public static final int MAXLOOP = 40;
	
	MainActivity mActivity;

	public TodayPagerAdapter(FragmentManager fm, MainActivity activity) {
		super(fm);
		mActivity = activity;
	}

	@Override
	public Fragment getItem(int arg0) {
		return TodayPagerFragment.newInstance(arg0 % mActivity.mTodayList.size());
	}

	@Override
	public int getCount() {
		return mActivity.mTodayList.size() * MAXLOOP;
	}
	
	/**
	 * This hacking decrease app performance.
	 */
	@Override
	public Parcelable saveState() {
		return null;
//		return super.saveState();
	}
	
	@Override
	public Object instantiateItem(ViewGroup arg0, int arg1) {
		Log.w(getClass().getSimpleName(), "instantiateItem " + arg1);
		TodayPagerFragment fragment = (TodayPagerFragment) super.instantiateItem(arg0, arg1);
		try {
			fragment.loadData(mActivity.mTodayList.get(arg1));
		} catch (Throwable e) {
		}
		return fragment;
	}
	
	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}
}
